<template>
  <div class="container">
    <el-form :inline="true" :model="queryForm" size="small" ref="queryForm" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="queryForm.name" placeholder="登录账号/用户名称" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryForm.type" placeholder="请选择状态" style="width: 120px">
          <el-option v-for="item in typeSelect" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" round @click="query()">搜索</el-button>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button v-if="isAuth('sys:user:add')" type="success" icon="el-icon-plus" size="small" @click="add()">新增</el-button>
        <!--<el-button v-if="isAuth('sys:user:switch')" type="danger" icon="el-icon-close" size="small" @click="batchStop()">批量停用</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;"
      border
      @selection-change="handleSelectionChange"
      :height="countHeight"
      v-loading="tableLoading"
    >
      <!--<el-table-column header-align="center" type="selection" width="46" align="center"></el-table-column>-->
      <el-table-column header-align="center" prop="userStatus" label="状态" align="center" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userStatus === 0" size="small" type="danger" effect="dark">禁用</el-tag>
          <el-tag v-else-if="scope.row.userStatus === 1" size="small" type="success" effect="dark">正常</el-tag>
          <el-tag v-else size="small" type="warning" effect="dark">锁定</el-tag>
        </template>
      </el-table-column>
      <el-table-column header-align="center" prop="userName" label="登录账号" width="120"></el-table-column>
      <el-table-column header-align="center" prop="userAlias" label="用户名称" width="120"></el-table-column>
      <el-table-column header-align="center" prop="userPhone" label="手机号码" width="120" align="center"></el-table-column>
      <el-table-column header-align="center" prop="userEmail" label="邮箱地址" show-overflow-tooltip></el-table-column>
      <el-table-column header-align="center" prop="userDesc" label="描述" show-overflow-tooltip></el-table-column>
      <!--<el-table-column header-align="center" prop="roleNames" label="角色" show-overflow-tooltip>-->
        <!--<template slot-scope="scope">-->
          <!--<span v-html="formatColArray(scope.row.roleNames)"></span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column header-align="center" prop="createTime" label="创建时间" align="center" width="120"></el-table-column>
      <el-table-column header-align="center" align="center" fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-tooltip content="查看详情" placement="top">
            <el-button size="mini" icon="el-icon-search" circle @click="handleDetail(scope.row.userId)"></el-button>
          </el-tooltip>
          <el-tooltip content="编辑" v-if="isAuth('sys:user:edit')" placement="top">
            <el-button size="mini" type="primary" icon="el-icon-edit" circle @click="handleEdit(scope.row.userId)"></el-button>
          </el-tooltip>
          <el-tooltip content="停用"  v-if="isAuth('sys:user:switch') && scope.row.userStatus === 1" placement="top">
            <el-button size="mini" type="danger" icon="el-icon-close" circle @click="handleStatus(scope.row.userId, scope.row.userStatus)"></el-button>
          </el-tooltip>
          <el-tooltip content="启用" v-if="isAuth('sys:user:switch') && scope.row.userStatus === 0" placement="top" >
            <el-button size="mini" type="success" icon="el-icon-check" circle @click="handleStatus(scope.row.userId, scope.row.userStatus)"></el-button>
          </el-tooltip>
          <el-tooltip content="设置角色" v-if="isAuth('sys:user:auth')" placement="top">
            <el-button size="mini" type="warning" icon="el-icon-s-custom" circle @click="handleRoles(scope.row.userId)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      width="100%"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 模态框 用户 新增/编辑/查看详情 -->
    <dialog-user ref="dialogUser" @refreshData="getDataList"></dialog-user>
    <!-- 模态框 用户角色关系 授权 -->
    <dialog-user-role-rel ref="dialogUserRoleRel"></dialog-user-role-rel>
  </div>
</template>
<script>

  import userDialog from './dialog/userModule.vue'
  import userRoleRelDialog from './dialog/userRoleRelModule.vue'
  import { isAuth } from '@/utils'

  export default {
    name: 'zb-user',
    data () {
      return {
        tableLoading: false,
        tableData: [], // 表数据
        pageIndex: 1, //当前页码
        pageSize: 10,
        totalPage: 0,
        multipleSelection: [], // 表复选框选择
        typeSelect:[
          {value: '',label: '全部状态'},
          {value: 0, label: '禁用'},
          {value: 1, label: '正常'},
          {value: 2, label: '锁定'}
        ],
        queryForm: {name: '',type: ''} // 搜索框表单
      }
    },
    activated () {
      this.getDataList()
    },
    computed: {
      countHeight() {
        return window.screen.availHeight - 340
      }
    },
    methods: {
      getDataList(){
        if (!isAuth('sys:user:view')) {
          return
        }
        const data = {
          page: this.pageIndex,
          limit: this.pageSize,
          name: this.queryForm.name,
          type: this.queryForm.type
        }
        this.tableLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/user/list'),
          method: 'get',
          params: this.$http.adornParams(data)
        }).then(response => {
          const result = response.data
          if (result && result.code === 200) {
              this.tableData = result.data.dataList
              this.totalPage = result.data.totalCount
            } else {
              this.$message({message: result.msg, type: 'error'})
            }
          this.tableLoading = false
        }).catch(err => {
          this.$message({message: err, type: 'error'})
          this.tableLoading = false
        })
      },
      // 分页sizes选择触发事件
      handleSizeChange(val) {
        this.pageSize = val
        this.getDataList()
      },
      // 分页jumper选择触发事件
      handleCurrentChange(val) {
        this.pageIndex = val
        this.getDataList()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 搜索 条件查询-登录账号/用户名
      query() {
        this.pageIndex = 1
        this.pageSize = 10
        this.getDataList()
      },
      // 新增
      add() {
        const params = {type: 'add'}
        this.$nextTick(() => {
          this.$refs['dialogUser'].init(params)
        })
      },
      // 批量停用
//      batchStop() {
//        const ids = []
//        this.multipleSelection.forEach(function (item) {
//          if(item.userStatus == 1){
//            ids.push(item.userId)
//          }
//        })
//        if (ids.length === 0) {
//          this.$message({message: '没有需要停用的用户!', type: 'info'})
//          return
//        }
//        this.$http({
//          url: this.$http.adornUrl('/sys/user/delBatch'),
//          method: 'post',
//          data: this.$http.adornData({ids: ids},false)
//        }).then(response => {
//          const result = response.data
//          if (result && result.code === 0) {
//            if (result.success){
//              this.getDataList()
//              this.$message({message: result.msg, type: 'success'})
//            } else {
//              this.$message({message: result.msg, type: 'error'})
//            }
//          }
//        }).catch(reason => {
//          this.$message({message: reason, type: 'error'})
//        })
//      },
      // 查看详情
      handleDetail(id) {
        const params = {type: 'detail', id: id}
        this.$nextTick(() => {
          this.$refs['dialogUser'].init(params)
        })
      },
      // 编辑
      handleEdit(id) {
        const params = {type: 'edit', id: id}
        this.$nextTick(() => {
          this.$refs['dialogUser'].init(params)
        })
      },
      // 停用/启用
      handleStatus(id, status) {
        const tips = status ? '停用' : '启用'
        this.$confirm('是否' + tips + '该用户?', '提示', {type: 'warning'}).then(() => {
          const data = {
            status: status ? 0 : 1
          }
          this.$http({
            url: this.$http.adornUrl(`/sys/user/status/${id}`),
            method: 'put',
            data: this.$http.adornData(data)
          }).then(response => {
            const result = response.data
            if (result && result.code === 200) {
              this.getDataList()
              this.$message({message: tips + result.msg, type: 'success'})
            } else {
              this.$message({message: tips + result.msg, type: 'error'})
            }
          })
        }).catch(() => {})
      },
      // 角色授权
      handleRoles(id){
        this.$nextTick(() => {
          this.$refs['dialogUserRoleRel'].init(id)
        })
      },
      formatColArray(arr) {
        return arr.join('，')
      }
    },
    components: {
      'dialog-user': userDialog,
      'dialog-user-role-rel': userRoleRelDialog
    }
  }
</script>
<style>
</style>
